<template>
  <el-card class="spgl">
    <div class="spgl2">
      <div class="head">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane class="qb-tab" :label="`全部(${upperCount + downCount})`" name="0" />
          <el-tab-pane class="qb-tab2" :label="`已上架(${upperCount})`" name="1" />
          <el-tab-pane class="qb-tab3" :label="`未上架(${downCount})`" name="2" />
		 </el-tabs>
		 <div class="qb-top">
			  <el-button type="primary" @click="add">新建商品</el-button>
			  <el-button type="primary" :disabled="multipleSelection.length === 0" @click="handleSetGoodsShelf">{{ activeName === '1'?'批量下架':'批量上架' }}</el-button>
			  <el-button type="danger" :disabled="multipleSelection.length === 0" @click="handleDelGoodsInfo">批量删除</el-button>
		   </div>
       
       <el-table
	   :data="tableData"
	   v-loading="tableLoading"
		width="100%"
		border
		stripe
		height="640px"
		style="margin-top: 10px;"
		 @selection-change="handleSelectionChange"
       :default-sort="{ prop: 'date', order: 'descending' }"
     >
       <el-table-column type="selection" width="55"> </el-table-column>
       <el-table-column label="商品名称" width="360" align="center"
        >         
         <template slot-scope="scope">     
           <p style="margin:0">{{scope.row.goods_name }}</p>
           <div style="display:flex;">
             <img style="width:80px;height:60px" :src="configUrl + scope.row.goods_img" alt="">
             <p style="line-height:80px;margin:0">{{scope.row.goods_sku }}</p>
           </div>
         </template>
       </el-table-column>
       <el-table-column prop="price" label="价格" sortable min-width="15%" align="center">
		   <template slot-scope="scope">
		     <p style="margin:0">￥ {{scope.row.price }}</p>
			 </template>
       </el-table-column>
       <el-table-column prop="stock" label="库存" sortable min-width="15%" align="center">
       </el-table-column>
       <el-table-column prop="sale_num" label="销量" sortable min-width="15%" align="center">
       </el-table-column>
       <el-table-column prop="create_time" label="创建时间" sortable min-width="16%" align="center">
       </el-table-column>
        <el-table-column prop="shelf_status" label="状态" sortable min-width="10%" align="center">
			<template slot-scope="scope">
			  <p style="margin:0">{{scope.row.shelf_status ===1?'上架':'下架' }}</p>
			 </template>
       </el-table-column>
       <el-table-column label="操作" width="100" align="center">
         <template slot-scope="scope">
           <div>
              <el-button
             @click="handleClick2(scope.row)"
             type="text"
             size="small"
             >编辑</el-button>
           </div>
          <div>
            <el-button type="text" size="small" style="margin-left: 0" @click="setGoodsShelf(scope.row.goods_id,scope.row.shelf_status ===1?2:1)">
				{{scope.row.shelf_status ===2?'上架':'下架' }}
			</el-button>
          </div>
           <div>
             <el-button type="text" size="small" style="margin-left: 0" @click="delGoodsInfo(scope.row.goods_id)">删除</el-button>
           </div>
         </template>
       </el-table-column>
     </el-table>
	     
		 <el-pagination
		   class="page-pagination"
		   background
		   @size-change="handleSizeChange"
		   @current-change="handleCurrentChange"
		   :current-page="params.page_num"
		   :page-sizes="[10, 20, 50, 100]"
		   :page-size="params.page_size"
		   layout="total, sizes, prev, pager, next, jumper"
		   :total="all_data_num">
		 </el-pagination>
		 
        <div class="head-right">
          <el-input 
		  class="sp-name" 
		  v-model="params.keywords"
		  clearable
		  placeholder="请输入商品名称/商品ID" />
          <el-form ref="form" :model="form">
            价格<el-form-item>
              <el-input style="width:60px;height:38px" v-model="priceB"/>
            </el-form-item>
            ~
            <el-form-item>
              <el-input style="width:60px;height:38px !important"  v-model="priceE"/>
            </el-form-item>
          </el-form>
          <el-button class="btn" type="primary" @click="searchData">搜索</el-button>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import { pageMixin } from '@/components/PageMixin/index'
export default {
  mixins: [ pageMixin ],
  data() {
    return {
	  tableLoading: false,
      activeName: "0",
      formatter:"",
      input:"",
	  upperCount: 0,
	  downCount: 0,
      form: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },
	   params:{
		  page_size:10,
		  page_num:1,
		  shelf_status: 0,
		  keywords: '',
		  range_price: ''
	   },
	   priceB: '',
	   priceE: '',
    };
  },
  watch:{
	  range_price(){
		  this.params.range_price = this.range_price
	  }
  },
  computed:{
	  range_price(){
		  if(this.priceB === '' || this.priceE === ''){
			  return ''
		  }
		  return `${this.priceB}-${this.priceE}`
	  }
  },
  methods: {
    getData(){
		this.params['token'] = this.$getToken()
		this.tableLoading = true
		this.service({
		  method: "post",
		  url: "/api/admin/getGoodsList",
		  data:this.params
		}).then(res => {
		 this.tableData = res.data.list;
		 
		 this.upperCount = res.data.upperCount
		 this.downCount = res.data.downCount
		 this.all_data_num = res.data.all_data_num
		 
		 
		 
		 setTimeout(()=>{
			this.tableLoading = false 
		 },500)
		});
	},
    handleClick() {
		this.params.shelf_status = Number(this.activeName)
		this.multipleSelection = []
        this.getData()
    },
    add(){
      this.$router.push({path:"/example/editshop", query: {
      	  method: 'create',
      }})
    },
    // edit(){
    //   this.$router.push({path:"/example/editshop"})
    // },
    handleClick2(row) {
      this.$router.push({path:"/example/editshop", query: {
		  id: row.goods_id,
		  method: 'update',
	  }})
    },
	handleSetGoodsShelf(){
		let goods_ids = this.multipleSelection.map(item =>{
			return item.goods_id
		})
		goods_ids = goods_ids.join(",")
		let shelf_status = 1
		if (this.activeName === '1'){
			shelf_status = 2
		}
		this.setGoodsShelf(goods_ids, shelf_status)
	},
	setGoodsShelf(goods_id, shelf_status){
		this.tableLoading = true
		this.service({
		  method: "post",
		  url: "/api/admin/setGoodsShelf",
		  data:{
			  goods_id,
			  shelf_status,
			  token: this.$getToken()
		  }
		}).then(res => {
		   if (res.error_code === 200){
			   this.getData()
		   }	
		});
	},
	handleDelGoodsInfo(){
		let goods_ids = this.multipleSelection.map(item =>{
			return item.goods_id
		})
		goods_ids = goods_ids.join(",")
		this.delGoodsInfo(goods_ids)
	},
	delGoodsInfo(goods_id){
		this.tableLoading = true
		this.service({
		  method: "post",
		  url: "/api/admin/delGoodsInfo",
		  data:{
			  goods_id,
			  token: this.$getToken()
		  }
		}).then(res => {
		   if (res.error_code === 200){
			   this.getData()
		   }	
		});
	},
  }
};
</script>

<style lang="scss" scoped>
.spgl {
  width: 98%;
  // height: 98%;
  height: 860px;
  margin: 10px;
  .spgl2 {
    .head {
      width: 100%;
      height: 60px;
      position: relative;
      .el-tabs,
      .el-tabs--top {
        width: 100%;
        margin-right: 10%;
        .qb-tab,.qb-tab2,.qb-tab3 {
          // width: 250%;
          overflow: hidden;
          .qb-top {
            // width: 40%;
            height: 40px;
            display: flex;
            font-size: 14px;
            margin-bottom: 15px;
            .p1 {
              width: 105px;
              height: 40px;
              line-height: 40px;
              text-align: center;
              margin: 0 10px 0 0;
              background-color: #f0f0f0;
            }
          }
          .content {
            width: 100%;
            height: 100%;

            .content2 {
              width: 100%;
              height: 430px;
              font-size: 14px;
              display: flex;
              justify-content: space-around;
              .con-title {
                width: 20%;
                height: 100%;
                .item-title {
                  height: 50px;
                  line-height: 50px;
                }
                .item {
                  height: 55px;
                  display: flex;
                  .el-checkbox {
                    line-height: 55px;
                  }
                  .item-right {
                    margin: 0 0 0 10px;
                    .p2 {
                      margin: 0 0 5px 0;
                    }
                    .r-b {
                      display: flex;
                      .img1 {
                        width: 18px;
                        height: 34px;
                        margin-right: 10px;
                      }
                      .p3 {
                        margin: 0;
                      }
                    }
                  }
                }
              }
              .con-price,
              .con-kucun,
              .con-xiaoliang,
              .con-time,
              .con-zhuangtai {
                width: 8%;
                height: 100%;
                .p4 {
                  line-height: 50px;
                  margin: 0;
                }
                .p5 {
                  line-height: 55px;
                  margin: 0;
                }
              }
              .con-time {
                width: 10%;
                .p5 {
                  line-height: 20px;
                }
              }
              .con-caozuo {
                width: 8%;
                .p4 {
                  line-height: 50px;
                  margin: 0;
                }
                .p5,
                .p6 {
                  line-height: 18px;
                  margin: 0;
                }
                .p6 {
                  color: #fd804a;
                }
              }
            }
            .fenye{
              width: 100%;
              .el-pagination,.is-background {
              width: 50%;
              margin-left: 50%;
            }
            }
            
          }
        }
      }
      .head-right {
        position: absolute;
        right: 0;
        top: 0px;
        width: 50%;
        display: flex;
        justify-content: space-between;
        overflow: hidden;
        .sp-name {
          width: 240px;
          height: 38px;
          line-height: 38px;
          text-align: center;
          background-color: #f0f0f0;
          font-size: 14px;
        }
        .el-form {
          width: 240px;
          height: 38px;
          line-height: 38px;
          display: flex;
        }
        .btn {
          width: 90px;
          height: 38px;
          background: #2292e8;
          opacity: 1;
        }
      }
    }
  }
}

.page-pagination{
	text-align: right;
	margin-top: 10px;
}
</style>